package com.ctn.information.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ctn.information.R;
import com.ctn.information.entity.Information;
import com.ctn.information.sycn.AsyncImageLoader;
import com.ctn.information.sycn.AsyncImageLoader.ImageCallback;
import com.ctn.information.sycn.ImageCacher;
import com.ctn.information.view.xlistview.XListView;

public class NewsListAdapter extends BaseAdapter {
	private ListView listView;
	private AsyncImageLoader asyncImageLoader;
	private List<Information> list;
	private LayoutInflater mInflater;
	private Context currentContext;

	public NewsListAdapter(Context context, List<Information> list, XListView listView) {
		currentContext = context;
		this.listView = listView;
		asyncImageLoader = new AsyncImageLoader(context);
		this.list = list;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		Information entity = list.get(position);
		if (convertView != null && parent.getId() == R.id.xListView) {
			viewHolder = (ViewHolder) convertView.getTag();
		} else {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item, null);

			viewHolder.remoteImage = (ImageView) convertView
					.findViewById(R.id.remote_image);
			viewHolder.newsTitle = (TextView) convertView
					.findViewById(R.id.news_title);
			viewHolder.newsContent = (TextView) convertView
					.findViewById(R.id.news_content);
			viewHolder.newsTimeStemp = (TextView) convertView
					.findViewById(R.id.new_timestemp);
		}
		String tag = entity.getImageUrl();
		viewHolder.remoteImage.setTag(tag);
		Drawable cachedImage = asyncImageLoader.loadDrawable(
				ImageCacher.EnumImageType.Avatar, tag, new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable, String tag) {
						Log.i("Drawable", tag);
						ImageView imageViewByTag = (ImageView) listView
								.findViewWithTag(tag);
						if (imageViewByTag != null && imageDrawable != null) {
							imageViewByTag.setImageDrawable(imageDrawable);
						} else {
							try {
								imageViewByTag
										.setImageResource(R.drawable.favorites);
							} catch (Exception ex) {

							}
						}
					}
				});
		viewHolder.remoteImage
				.setImageResource(R.drawable.abs__ab_bottom_transparent_light_holo);
		if (cachedImage != null) {
			viewHolder.remoteImage.setImageDrawable(cachedImage);
		}

		viewHolder.newsTitle.setText(entity.getNewsTitle());
		viewHolder.newsContent.setText(entity.getNewsContent());
		viewHolder.newsTimeStemp.setText(entity.getTimeStemp().toString());
		convertView.setTag(viewHolder);
		return convertView;
	}
	/**
	 * �õ����
	 * 
	 * @return
	 */
	public List<Information> GetData() {
		return list;
	}
	/**
	 * ����
	 * 
	 * @param list
	 */
	public void InsertData(List<Information> list) {
		this.list.addAll(0, list);
		this.notifyDataSetChanged();
	}
	/**
	 * �������
	 * 
	 * @param list
	 */
	public void AddMoreData(List<Information> list) {
		this.list.addAll(list);
		this.notifyDataSetChanged();
	}
	/**
	 * �Ƴ����
	 * @param entity
	 */
	public void RemoveData(Information entity){
		for(int i=0,len=this.list.size();i<len;i++){
			if(this.list.get(i).getId()==entity.getId()){
				this.list.remove(i);
				this.notifyDataSetChanged();
				break;
			}
		}
	}
	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	public class ViewHolder {
		ImageView remoteImage;
		TextView newsTitle;
		TextView newsContent;
		TextView newsTimeStemp;
	}
}
